package com.lwj.liwanjia.view.activity.work.shang_jia.spfabu;


import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.AddAdapter;
import com.lwj.liwanjia.adapter.fabu.FabuFzAdapter;
import com.lwj.liwanjia.adapter.fabu.FabuJyAdapter;
import com.lwj.liwanjia.adapter.fabu.FabuTabAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.fabu.EditFabuBean;
import com.lwj.liwanjia.bean.fabu.FabuBbBean;
import com.lwj.liwanjia.bean.fabu.FabuBean;
import com.lwj.liwanjia.bean.fabu.FabuInfoBean;
import com.lwj.liwanjia.bean.fabu.FabuTabBean;
import com.lwj.liwanjia.bean.fabu.FabuTbBean;
import com.lwj.liwanjia.bean.fabu.FzBean;
import com.lwj.liwanjia.bean.fabu.FzSubBean;
import com.lwj.liwanjia.bean.fabu.JyBean;
import com.lwj.liwanjia.bean.fabu.JySupBean;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.impl.EditFabuView;
import com.lwj.liwanjia.impl.FabuBbView;
import com.lwj.liwanjia.impl.FabuInforView;
import com.lwj.liwanjia.impl.FabuTbView;
import com.lwj.liwanjia.impl.FabuView;
import com.lwj.liwanjia.presenter.EditFabuPresenter;
import com.lwj.liwanjia.presenter.FabuBbPresenter;
import com.lwj.liwanjia.presenter.FabuInfoPresenter;
import com.lwj.liwanjia.presenter.FabuPresneter;
import com.lwj.liwanjia.presenter.FbTbUploadPresenter;
import com.lwj.liwanjia.utils.BitmapUtils;
import com.lwj.liwanjia.utils.BitmapWriteUtils;
import com.lwj.liwanjia.utils.InputImageDialog;
import com.lwj.liwanjia.utils.MyRecyclerView;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.utils.UploadImageDialog;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.Constants;

public class JyFabuActivity extends BaseActivity implements FabuTbView, FabuBbView, FabuView, FabuInforView, EditFabuView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rlv_fabu)
    SuperRecyclerView rlvFabu;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private FabuJyAdapter fabuJyAdapter;
    private List<JyBean> list = new ArrayList<>();//规格集合
    private List<FabuTabBean> tabList = new ArrayList<>();//标签集合
    private HeadViewHolder headHolder;
    private FooterViewHolder footerHolder;
    private FbTbUploadPresenter tbUploadPresenter;//商品图标上传
    private UploadImageDialog tbDiaolg;//图片上传
    private String imageUrl;
    private String imageType;
    private String commodityimages;//商品图标url
    private String particularphoto;//商品描述（详情）url
    private InputImageDialog imageDialog;//多张图片上传
    private ImageView addIg;//多张图片添加的按钮
    private AddAdapter addAdapter;//多张图片展示的适配器
    private List<String> imageList = new ArrayList<>();//宝贝多张展示图
    private List<File> files = new ArrayList<>();//上传多张图片
    private FabuBbPresenter bbPresenter;//宝贝描述图片上传
    private Map<String, Object> map = new HashMap<>();//发布时上传的字段
    private FabuPresneter fabuPresneter;//发布
    private ArrayList<Integer> idList;
    private JySupBean jySupBean;//上传规格
    private FabuInfoPresenter infoPresenter;//查看商品信息
    private String commodityBatch;
    private FabuTabAdapter tabAdapter;//标签
    private int clickRight;//编辑、保存
    private EditFabuPresenter editPresenter;//修改商品信息
    private String commodityslideimgId;
    private String clickType = "编辑";//区分是保存还是发布

    @Override
    protected void initView() {
        commodityBatch = getIntent().getStringExtra("commodityBatch");
        clickRight = getIntent().getIntExtra("type", 0);
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("我的发布");
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlvFabu.setLayoutManager(manager);
        rlvFabu.setRefreshEnabled(false);
        rlvFabu.setLoadMoreEnabled(false);
        rlvFabu.setItemAnimator(new DefaultItemAnimator());
        fabuJyAdapter = new FabuJyAdapter(this, list);
        addView();
        rlvFabu.setAdapter(fabuJyAdapter);
        listener();
        tbUploadPresenter = new FbTbUploadPresenter(this);
        bbPresenter = new FabuBbPresenter(this);
        fabuPresneter = new FabuPresneter(this);
        infoPresenter = new FabuInfoPresenter(this);
        editPresenter = new EditFabuPresenter(this);
    }

    //设置所有的控件不可以点击
    private void setEnable(boolean flag) {
        if (commodityBatch != null && !TextUtils.isEmpty(commodityBatch)) {
            headHolder.etSpName.setEnabled(flag);
            headHolder.llType.setClickable(flag);
            footerHolder.etDanwei.setEnabled(flag);
            footerHolder.llPeisong.setClickable(flag);
            footerHolder.edMiaoshu.setEnabled(flag);
            fabuJyAdapter.setEditEnable(flag);//规格
            tabAdapter.setClickEnable(flag);//标签
            addAdapter.setCancleEnable(flag);//宝贝描述图片
            if (!flag) {//不可点击
                tvRight.setVisibility(View.VISIBLE);
                tvRight.setText("编辑");
                footerHolder.tbCancle.setVisibility(View.GONE);
                footerHolder.xqCancle.setVisibility(View.GONE);
                footerHolder.tvFabu.setVisibility(View.GONE);
                addIg.setVisibility(View.GONE);
                footerHolder.tvTabGeshi.setVisibility(View.GONE);
                footerHolder.tvMiaoshuGeshi.setVisibility(View.GONE);
                footerHolder.tvXqGeshi.setVisibility(View.GONE);
            } else {
                tvRight.setText("保存");
                addIg.setVisibility(View.VISIBLE);
                footerHolder.tvFabu.setVisibility(View.VISIBLE);
                footerHolder.tbCancle.setVisibility(View.VISIBLE);
                footerHolder.xqCancle.setVisibility(View.VISIBLE);
                footerHolder.tvTabGeshi.setVisibility(View.VISIBLE);
                footerHolder.tvMiaoshuGeshi.setVisibility(View.VISIBLE);
                footerHolder.tvXqGeshi.setVisibility(View.VISIBLE);
            }

        }
    }

    private void listener() {
        //每个EditText文本的回调监听
        fabuJyAdapter.setOnTextChangeListener(new FabuJyAdapter.onTextChangeListener() {
            @Override
            public void onTextChanged(String type, int pos, String str) {
                JyBean jyBean = list.get(pos);
                switch (type) {
                    case "宝贝价格":
                        jyBean.setCommodityprice(str);
                        break;
                    case "折扣价":
                        jyBean.setCommodityvipprice(str);
                        break;
                    case "库存量":
                        jyBean.setStock(str);
                        break;
                    case "颜色":
                        jyBean.setColour(str);
                        break;
                }
                if (!TextUtils.isEmpty(jyBean.getColour()) && !TextUtils.isEmpty(jyBean.getStock())
                        && !TextUtils.isEmpty(jyBean.getCommodityvipprice()) && !TextUtils.isEmpty(jyBean.getCommodityprice())) {
                    jyBean.setEditAll(true);
                } else {
                    jyBean.setEditAll(false);
                }
            }

        });
        //删除规格
        fabuJyAdapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_delete_item://删除item
                        if (list.size() == 1) {
                            Toast.makeText(JyFabuActivity.this, "已经是最后一个了", Toast.LENGTH_SHORT).show();
                        } else {
                            fabuJyAdapter.removeData(position);
                        }
                        break;
                }
            }
        });

    }

    private void addView() {
        View headView = LayoutInflater.from(this).inflate(R.layout.fabu_head, null);
        headHolder = new HeadViewHolder(headView);
        initHeadView();
        fabuJyAdapter.addHeaderView(headView);
        View footerView = LayoutInflater.from(this).inflate(R.layout.fabu_footer, null);
        footerHolder = new FooterViewHolder(footerView);
        initFooterView();
        fabuJyAdapter.addFooterView(footerView);
    }

    //初始化头部控件
    private void initHeadView() {
        //选择宝贝类型
        headHolder.llType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(JyFabuActivity.this, TypeActivity.class);
                startActivityForResult(intent, 333);
            }
        });
    }

    //初始化尾部控件
    private void initFooterView() {
        //添加规格
        footerHolder.llAddItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JyBean jyBean = list.get(list.size() - 1);
                if (jyBean.isEditAll()) {
                    fabuJyAdapter.addData(list.size());
                } else {
                    Toast.makeText(JyFabuActivity.this, "数据必须全部填写", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //选择配送方式
        footerHolder.llPeisong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(JyFabuActivity.this, ListActivity.class);
                startActivityForResult(intent, 444);
            }
        });
        //标签选择
        FlexboxLayoutManager tabManager = new FlexboxLayoutManager();
        //设置主轴排列方式
        tabManager.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        tabManager.setFlexWrap(FlexWrap.WRAP);
        tabManager.setAlignItems(AlignItems.STRETCH);
        tabManager.setJustifyContent(JustifyContent.FLEX_START);
        footerHolder.rlvFlex.setLayoutManager(tabManager);
        tabAdapter = new FabuTabAdapter(this, tabList);
        footerHolder.rlvFlex.setAdapter(tabAdapter);
        tabAdapter.setListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                boolean check = tabList.get(position).isCheck();
                if (check) {
                    tabList.get(position).setCeck(false);
                } else {
                    tabList.get(position).setCeck(true);
                }
                tabAdapter.notifyDataSetChanged();
            }
        });
        //商品图标上传
        footerHolder.ivSpTubiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageType = "商品图标";
                if (TextUtils.isEmpty(commodityimages)) {//没图片时添加图片
                    tbDiaolg = new UploadImageDialog(JyFabuActivity.this);
                    tbDiaolg.show();
                } else {//有图片时查看图片
                    setPhoto(commodityimages);
                }
            }
        });
        //取消图标
        footerHolder.tbCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commodityimages = "";
                footerHolder.tbCancle.setVisibility(View.GONE);
                Glide.with(JyFabuActivity.this).load(commodityimages).error(R.mipmap.tianjiaaaaaa).into(footerHolder.ivSpTubiao);
            }
        });
        //选择多张图片
        GridLayoutManager layoutManager = new GridLayoutManager(JyFabuActivity.this, 4);
        footerHolder.ivSpList.setLayoutManager(layoutManager);
        footerHolder.ivSpList.setRefreshEnabled(false);
        footerHolder.ivSpList.setLoadMoreEnabled(false);
        footerHolder.ivSpList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = 10;
                outRect.bottom = 10;
            }
        });
        addAdapter = new AddAdapter(JyFabuActivity.this, imageList);
        View igView = LayoutInflater.from(this).inflate(R.layout.add_item, null);
        addIg = (ImageView) igView.findViewById(R.id.iv_add);
        addAdapter.addFooterView(igView);
        footerHolder.ivSpList.setAdapter(addAdapter);
        addIg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commodityslideimgId = "";
                imageDialog = new InputImageDialog(JyFabuActivity.this);
                imageDialog.show();
            }
        });
        listener();
        //宝贝描述取消图片
        addAdapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                String pathName = imageList.get(position);
                String str = pathName.substring(pathName.lastIndexOf("/") + 1, pathName.length());
                imageList.remove(position);
                for (int i = 0; i < files.size(); i++) {
                    if (files.get(i).getName().equals(str)) {
                        files.remove(i);
                    }
                }
                commodityslideimgId = "";
                if (imageList.size() < 4) {
                    addIg.setVisibility(View.VISIBLE);
                }
                addAdapter.notifyDataSetChanged();
            }
        });
        addAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(JyFabuActivity.this, PhotoViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                bundle.putStringArrayList("imageUrl", (ArrayList<String>) imageList);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        //详情(商品描述)页上传
        footerHolder.ivXiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageType = "商品描述";
                if (TextUtils.isEmpty(particularphoto)) {
                    tbDiaolg = new UploadImageDialog(JyFabuActivity.this);
                    tbDiaolg.show();
                } else {
                    setPhoto(particularphoto);
                }
            }
        });
        footerHolder.xqCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                particularphoto = "";
                footerHolder.xqCancle.setVisibility(View.GONE);
                Glide.with(JyFabuActivity.this).load(particularphoto).error(R.mipmap.tianjiaaaaaa).into(footerHolder.ivXiangqing);
            }
        });

        footerHolder.tvFabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initFabu();
            }
        });
    }

    //确定发布
    private void initFabu() {
        //宝贝名称
        String commodityname = headHolder.etSpName.getText().toString().trim();
        //宝贝类型
        StringBuffer desBuffer = new StringBuffer();
        if (idList != null) {
            for (int i = 0; i < idList.size(); i++) {
                desBuffer.append(idList.get(i) + ";");
            }
        }
        String description = desBuffer.toString();
        //单位
        String unit = footerHolder.etDanwei.getText().toString().trim();
        //配送方式
        String nowDistribution = footerHolder.tvPeisong.getText().toString();
        //标签
        StringBuffer tabStr = new StringBuffer();
        for (int i = 0; i < tabList.size(); i++) {
            if (tabList.get(i).isCheck()) {
                tabStr.append(tabList.get(i).getTabName() + ";");
            }
        }
        String modeofdistribution = tabStr.toString();
        //宝贝描述
        String commodityInfo = footerHolder.edMiaoshu.getText().toString();
        if (TextUtils.isEmpty(commodityname) || TextUtils.isEmpty(unit)
                || TextUtils.isEmpty(nowDistribution) || TextUtils.isEmpty(modeofdistribution)
                || TextUtils.isEmpty(commodityInfo)) {
            Toast.makeText(this, "关键数据不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        map.put("commodityname", commodityname);
        map.put("description", description);
        map.put("unit", unit);
        map.put("nowDistribution", nowDistribution);
        map.put("modeofdistribution", modeofdistribution);
        map.put("commodityInfo", commodityInfo);
        if (TextUtils.isEmpty(particularphoto) || TextUtils.isEmpty(commodityimages)) {
            Toast.makeText(this, "图标和详情页没有上传", Toast.LENGTH_SHORT).show();
            return;
        }
        map.put("particularphoto", particularphoto);
        map.put("commodityimages", commodityimages);
        map.put("token", SpfUtils.getSpfUtils(JyFabuActivity.this).getToken());
        jySupBean = new JySupBean();
        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).isEditAll()) {
                Toast.makeText(this, "规格没有全部填写", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (list.size() == 0) {
            Toast.makeText(this, "还没有选择宝贝类型", Toast.LENGTH_SHORT).show();
            return;
        }
        jySupBean.setCommodityDataFormat(list);
        //上传宝贝描述图片
        if (TextUtils.isEmpty(commodityslideimgId)) {//是否修改了宝贝描述图片
            if (files.size() > 0) {
                bbPresenter.uploadBb(files, SpfUtils.getSpfUtils(JyFabuActivity.this).getToken());
            } else {
                Toast.makeText(JyFabuActivity.this, "请先选择宝贝描述图片", Toast.LENGTH_SHORT).show();
            }
        } else {//编辑
            map.put("commodityslideimgId", commodityslideimgId);//保存商品图片id
            map.put("description", "0");
            map.put("commodityBatch", commodityBatch);
            editPresenter.editFabuData(map, jySupBean);
        }
    }

    //有图片时放大图片
    public void setPhoto(String url) {
        Intent intent = new Intent(JyFabuActivity.this, PhotoViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("position", 0);
        bundle.putString("url", url);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    protected void initData() {
        getTabData();
        //查看商品信息
        if (commodityBatch != null && !TextUtils.isEmpty(commodityBatch)) {
            if (clickRight == 0) {
                setEnable(false);
            } else {
                tvRight.setVisibility(View.VISIBLE);
                setRight();
            }
            infoPresenter.getFabuData(SpfUtils.getSpfUtils(this).getToken(), commodityBatch);
        }
    }

    //添加标签数据
    private void getTabData() {
        FabuTabBean tabBean = new FabuTabBean("7天无理由退货", false);
        FabuTabBean tabBean1 = new FabuTabBean("货到付款", false);
        FabuTabBean tabBean2 = new FabuTabBean("进口商品", false);
        FabuTabBean tabBean3 = new FabuTabBean("全国联保", false);
        FabuTabBean tabBean4 = new FabuTabBean("正品保障", false);
        tabList.add(tabBean);
        tabList.add(tabBean1);
        tabList.add(tabBean2);
        tabList.add(tabBean3);
        tabList.add(tabBean4);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fz_fabu;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 333) {//宝贝类型
                String type = data.getStringExtra("type");
                idList = data.getIntegerArrayListExtra("idList");
                headHolder.tvSpType.setText(type);
                if (list.size() == 0) {
                    //显示出第一个规格
                    JyBean jyBean = new JyBean();
                    list.add(jyBean);
                    fabuJyAdapter.notifyDataSetChanged();
                    //添加规格按钮提现出来
                    footerHolder.llAddItem.setVisibility(View.VISIBLE);
                }
            } else if (requestCode == 444) {//配送方式
                String fangshi = data.getStringExtra("fangshi");
                footerHolder.tvPeisong.setText(fangshi);
            } else if (requestCode == UploadImageDialog.CAMERA_REQUEST_CODE) {
                imageUrl = tbDiaolg.cameraPath;
                File file = compressImage(imageUrl);
                tbUploadPresenter.uploadTb(file, SpfUtils.getSpfUtils(JyFabuActivity.this).getToken());
            } else if (requestCode == UploadImageDialog.ALBUM_REQUEST_CODE) {//单张图片
                Uri uri = data.getData();
                String[] proj = {MediaStore.Images.Media.DATA};
                //查询本地图片
                Cursor cursor = managedQuery(uri, proj, null, null,
                        null);
                // 这个是获得用户选择的图片的索引值
                int column_index = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                // 将光标移至开头 ，这个很重要，不小心很容易引起越界
                cursor.moveToNext();
                // 最后根据索引值获取图片路径
                imageUrl = cursor.getString(column_index);
                File file = compressImage(imageUrl);
                tbUploadPresenter.uploadTb(file, SpfUtils.getSpfUtils(JyFabuActivity.this).getToken());
            } else if (requestCode == InputImageDialog.CAMERA_REQUEST_CODE) {//多张图片上传
                String imagePath = imageDialog.cameraPath;
                if (imageList.size() < 4) {
                    imageList.add(imagePath);
                    files.add(compressImage(imagePath));
                }
                if (imageList.size() == 4) {
                    addIg.setVisibility(View.GONE);
                }
            } else if (requestCode == InputImageDialog.ALBUM_REQUEST_CODE) {//多张图片上传
                ArrayList<String> imageUrl = data.getStringArrayListExtra("imageUrl");
                for (int i = 0; i < imageUrl.size(); i++) {
                    if (imageList.size() < 4) {
                        imageList.add(imageUrl.get(i));
                        files.add(compressImage(imageUrl.get(i)));
                    }
                    if (imageList.size() == 4) {
                        addIg.setVisibility(View.GONE);
                    }
                }
            }
            addAdapter.notifyDataSetChanged();
        }
    }

    //选完图片后压缩图片
    private File compressImage(String imageUrl) {
        String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.length());
        BitmapUtils.compressImageByPixel(imageUrl, fileName);
        File file = new File(Constants.IMAGE_PATH + fileName);
        return file;
    }

    @OnClick({R.id.tv_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                if (clickRight == 0) {
                    clickRight = 1;
                    setRight();
                } else if (clickRight == 1) {
                    clickRight = 0;
                    clickType = "保存";
                    initFabu();
                }
                break;
        }
    }

    private void setRight() {
        setEnable(true);
        footerHolder.tvFabu.setVisibility(View.GONE);//编辑时确定发布按钮不可见
        headHolder.llType.setClickable(false);//宝贝类型还是不可点击
        footerHolder.llAddItem.setVisibility(View.VISIBLE);//编辑时添加规格按钮出现
        if (imageList.size() >= 4) {
            addIg.setVisibility(View.GONE);//宝贝描述添加图片按钮
        }
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getFabuData(FabuInfoBean fabuInfoBean) {
        if (fabuInfoBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (fabuInfoBean.getState() == 1) {
            FabuInfoBean.DataBean data = fabuInfoBean.getData();
            setInfo(data);
        }
    }

    //设置宝贝信息
    private void setInfo(FabuInfoBean.DataBean data) {
        headHolder.etSpName.setText(data.getCommodityname());
        headHolder.tvSpType.setText(data.getDescription());
        List<FabuInfoBean.DataBean.ListBean> info = data.getList();
        setBaobei(info);
        footerHolder.etDanwei.setText(data.getUnit());
        footerHolder.tvPeisong.setText(data.getNowdistribution());
        setTab(data);
        commodityimages = data.getCommodityimages();
        particularphoto = data.getParticularphoto();
        commodityslideimgId = data.getCommodityslideimgId();
        Glide.with(this).load(commodityimages).error(R.mipmap.tianjiaaaaaa).into(footerHolder.ivSpTubiao);
        Glide.with(this).load(particularphoto).error(R.mipmap.tianjiaaaaaa).into(footerHolder.ivXiangqing);
        //将从网络获取到的图片重新存储到本地
        final List<String> slideImgs = data.getSlideImgs();
        imageList.clear();
        imageList.addAll(slideImgs);
        addAdapter.notifyDataSetChanged();
        files.clear();
        for (int i = 0; i < slideImgs.size(); i++) {
            final int finalI = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = BitmapWriteUtils.writeFile(slideImgs.get(finalI));
                    if (file != null) {
                        files.add(file);
                    }
                }
            }).start();
        }
        footerHolder.edMiaoshu.setText(data.getCommodityInfo());
    }

    //设置宝贝规格
    private void setBaobei(List<FabuInfoBean.DataBean.ListBean> info) {
        for (int i = 0; i < info.size(); i++) {
            JyBean jyBean = new JyBean();
            jyBean.setColour(info.get(i).getColour());
            jyBean.setCommodityprice(info.get(i).getCommodityprice());
            jyBean.setCommodityvipprice(info.get(i).getCommodityvipprice());
            jyBean.setStock(info.get(i).getStock());
            jyBean.setEditAll(true);
            list.add(jyBean);
        }
        fabuJyAdapter.notifyDataSetChanged();
    }

    //标签
    private void setTab(FabuInfoBean.DataBean data) {
        String[] tabs = data.getModeofdistribution().split(";");
        for (String tab : tabs) {
            switch (tab) {
                case "7天无理由退货":
                    tabList.get(0).setCeck(true);
                    break;
                case "货到付款":
                    tabList.get(1).setCeck(true);
                    break;
                case "进口商品":
                    tabList.get(2).setCeck(true);
                    break;
                case "全国联保":
                    tabList.get(3).setCeck(true);
                    break;
                case "正品保障":
                    tabList.get(4).setCeck(true);
                    break;
            }
        }
        tabAdapter.notifyDataSetChanged();
    }

    @Override
    public void editFabuData(EditFabuBean editFabuBean) {
        if (editFabuBean.getState() == 0) {
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
        } else if (editFabuBean.getState() == 1) {
            if (editFabuBean.getData().getStatus().equals("编辑成功")) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    //确定发布
    @Override
    public void submitFabu(FabuBean fabuBean) {
        if (fabuBean.getState() == 0) {
            Toast.makeText(this, "发布失败", Toast.LENGTH_SHORT).show();
        } else if (fabuBean.getState() == 1) {
            if (fabuBean.getData().getStatus().equals("发布成功")) {
                Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    //上传多张宝贝图片
    @Override
    public void uploadBb(FabuBbBean fabuBbBean) {
        if (fabuBbBean.getState() == 0) {
            Toast.makeText(this, "上传失败", Toast.LENGTH_SHORT).show();
        } else if (fabuBbBean.getState() == 1) {
            commodityslideimgId = fabuBbBean.getData().getId();
            if (TextUtils.isEmpty(commodityslideimgId)) {
                Toast.makeText(this, "宝贝描述图片上传没有成功", Toast.LENGTH_SHORT).show();
            } else {
                map.put("commodityslideimgId", commodityslideimgId);//保存商品图片id
                if (clickType.equals("保存")) {
                    map.put("description", "0");
                    map.put("commodityBatch", commodityBatch);
                    editPresenter.editFabuData(map, jySupBean);
                } else {
                    fabuPresneter.submitFabu(map, jySupBean);//发布
                }
            }
        }
    }

    //商品图标和商品描述上传
    @Override
    public void uploadTb(FabuTbBean fabuTbBean) {
        if (fabuTbBean.getState() == 0) {
            Toast.makeText(this, "上传图片失败！", Toast.LENGTH_SHORT).show();
        } else if (fabuTbBean.getState() == 1) {
            String url = fabuTbBean.getData().getUrl();
            if (imageType.equals("商品图标")) {
                commodityimages = url;
                if (!TextUtils.isEmpty(commodityimages)) {
                    footerHolder.tbCancle.setVisibility(View.VISIBLE);
                }
                Glide.with(JyFabuActivity.this).load(commodityimages).error(R.mipmap.tianjiaaaaaa).into(footerHolder.ivSpTubiao);
            } else if (imageType.equals("商品描述")) {
                particularphoto = url;
                if (!TextUtils.isEmpty(particularphoto)) {
                    footerHolder.xqCancle.setVisibility(View.VISIBLE);
                }
                Glide.with(JyFabuActivity.this).load(particularphoto).error(R.mipmap.tianjiaaaaaa).into(footerHolder.ivXiangqing);
            }
        }
    }

    static class HeadViewHolder {
        @BindView(R.id.et_sp_name)
        EditText etSpName;
        @BindView(R.id.tv_sp_type)
        TextView tvSpType;
        @BindView(R.id.ll_type)
        LinearLayout llType;

        HeadViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class FooterViewHolder {
        @BindView(R.id.ll_add_item)
        LinearLayout llAddItem;
        @BindView(R.id.et_danwei)
        EditText etDanwei;
        @BindView(R.id.tv_peisong)
        TextView tvPeisong;
        @BindView(R.id.ll_peisong)
        LinearLayout llPeisong;
        @BindView(R.id.rlv_flex)
        MyRecyclerView rlvFlex;
        @BindView(R.id.iv_sp_tubiao)
        ImageView ivSpTubiao;
        @BindView(R.id.tb_cancle)
        ImageView tbCancle;
        @BindView(R.id.ed_miaoshu)
        EditText edMiaoshu;
        @BindView(R.id.iv_sp_list)
        SuperRecyclerView ivSpList;
        @BindView(R.id.iv_xiangqing)
        ImageView ivXiangqing;
        @BindView(R.id.xq_cancle)
        ImageView xqCancle;
        @BindView(R.id.tv_fabu)
        TextView tvFabu;
        @BindView(R.id.tv_tab_geshi)
        TextView tvTabGeshi;
        @BindView(R.id.tv_miaoshu_geshi)
        TextView tvMiaoshuGeshi;
        @BindView(R.id.tv_xq_geshi)
        TextView tvXqGeshi;

        FooterViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bbPresenter != null) {
            bbPresenter = null;
        }
        if (tbUploadPresenter != null) {
            tbUploadPresenter = null;
        }
        if (fabuPresneter != null) {
            fabuPresneter = null;
        }
        if (infoPresenter != null) {
            infoPresenter = null;
        }
        if (editPresenter != null) {
            editPresenter = null;
        }
    }
}
